package com.ztf0337.mycurriculumdesign.ui.fragment;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.TextDate;
import com.ztf0337.mycurriculumdesign.adapter.ParkingSpaceAdapter;
import com.ztf0337.mycurriculumdesign.entity.ParkLot;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.service.ParkLotService;
import com.ztf0337.mycurriculumdesign.service.ParkingSpaceService;
import com.ztf0337.mycurriculumdesign.ui.activity.ParkingDetailActivity;

import java.util.List;
import java.util.Objects;

public class ParkingSpaceFragment extends Fragment {

    private RecyclerView rvParkingSpace;
    private ParkingSpaceAdapter adapter;
    private List<ParkingSpace> parkingSpaceList;
    private Context context;
    private TextView firstUse,toolbarText;
    private ImageView ic_more,ic_filter,ic_delete,ic_affirm,ic_cansel,ic_Search;
    private ParkingSpaceService parkingSpaceService;
    private Long parkLotId;
    private ParkLotService parkLotService;
    private EditText etSearch;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parking_space, container, false);
        context = getContext();
        if (context == null) {
            return view;
        }
        parkingSpaceService = new ParkingSpaceService(context);
        parkLotService = new ParkLotService(context);

        SharedPreferences optionParkLot = context.getSharedPreferences("optionParkLot", Context.MODE_PRIVATE);
        firstUse = view.findViewById(R.id.firstUse);

        ic_delete = requireActivity().findViewById(R.id.ic_delete);
        ic_delete.setVisibility(GONE);
        ic_affirm = requireActivity().findViewById(R.id.ic_affirm);
        ic_affirm.setVisibility(GONE);
        ic_cansel = requireActivity().findViewById(R.id.ic_cansel);
        ic_cansel.setVisibility(GONE);
        ic_more = requireActivity().findViewById(R.id.ic_more);
        ic_more.setVisibility(GONE);
        etSearch = requireActivity().findViewById(R.id.etSearch);
        etSearch.setText(""); // 清空内容
        ic_Search = requireActivity().findViewById(R.id.ic_Search);

        rvParkingSpace = view.findViewById(R.id.rvParkingSpace);
        rvParkingSpace.setLayoutManager(new LinearLayoutManager(getContext()));

        parkLotId = optionParkLot.getLong("parkLotId", -1L);
        ParkLot parkLotById = parkLotService.getParkLotById(parkLotId);

        LinearLayout searchLayout = requireActivity().findViewById(R.id.search);
        searchLayout.setVisibility(GONE);
        ic_filter = requireActivity().findViewById(R.id.ic_filter);
        if (parkLotById != null) {               //挂载fragment
            parkingSpaceList = parkingSpaceService.getParkingSpacesByParkLotId(parkLotId);//----------------根据停车场id获取停车位
            if (!parkingSpaceList.isEmpty()) {
                searchLayout.setVisibility(VISIBLE);
                firstUse.setVisibility(GONE);
                adapter = new ParkingSpaceAdapter(parkingSpaceList);
                rvParkingSpace.setAdapter(adapter);
                adapter.setOnItemClickListener((parkingSpace) -> {
                    Intent intent = new Intent(context, ParkingDetailActivity.class);
                    intent.putExtra("parkLotStatus",parkLotById.getStatus());
                    intent.putExtra("id", parkingSpace.getId());
                    Log.d("测试", "查看停车位详情id "+ parkingSpace.getId());
                    context.startActivity(intent);
                });
                ic_filter.setVisibility(VISIBLE);
            } else {
                ic_filter.setVisibility(GONE);
                firstUse.setText("无停车位信息");
                firstUse.setVisibility(VISIBLE);
            }
        } else {
            ic_filter.setVisibility(GONE);
            firstUse.setText("请选择停车位");
            firstUse.setVisibility(VISIBLE);
        }

        etSearch.setHint("请输入车位号");
        ic_Search.setOnClickListener(v -> {
            ic_cansel.setVisibility(GONE);
            ic_filter.setVisibility(VISIBLE);
            String search = etSearch.getText().toString().trim();
            if (search.isBlank()) {
                if (parkingSpaceList.isEmpty()) {
                    Toast.makeText(context, "无车位信息", Toast.LENGTH_SHORT).show();
                } else {
                    adapter.updateData(parkingSpaceList);
                }
                return;
            }
            List<ParkingSpace> parkingSpaces = parkingSpaceService.searchParkingSpaces(search,parkLotId);
            if (parkingSpaces.isEmpty()) {
                Toast.makeText(context, "无符合条件的车位", Toast.LENGTH_SHORT).show();
            } else {
                adapter.updateData(parkingSpaces);
            }
        });


        ic_filter.setOnClickListener(v -> {
            parkingSpaceFilter(parkLotId);
        });

        return view;
    }

    private void parkingSpaceFilter(Long parkLotId) {
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_parking_space_filter, null);

        RadioGroup statusGroup = dialogView.findViewById(R.id.statusGroup);
        RadioGroup typeGroup = dialogView.findViewById(R.id.typeGroup);


        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialogView)
                .setTitle("筛选条件")
                .setPositiveButton("确认", (dialog, which) -> {

                    int statusId = statusGroup.getCheckedRadioButtonId();
                    int typeId = typeGroup.getCheckedRadioButtonId();

                    // 保存当前选择

                    int status = 0,type = 0;

                    if (statusId == R.id.radioStatusIdle) {
                        status = 1;
                    } else if (statusId == R.id.radioStatusOccupied) {
                        status = 2;
                    }

                    if (typeId == R.id.radioSmallElectric) {
                        type = 1;
                    } else if (typeId == R.id.radioMediumElectric) {
                        type = 2;
                    } else if (typeId == R.id.radioLargeElectric) {
                        type = 3;
                    } else if (typeId == R.id.radioSmallFuel) {
                        type = 4;
                    } else if (typeId == R.id.radioMediumFuel) {
                        type = 5;
                    } else if (typeId == R.id.radioLargeFuel) {
                        type = 6;
                    }
                    // 调用你的筛选函数
                    filterData(status, type, parkLotId);
                    ic_filter.setVisibility(GONE);
                    ic_cansel.setVisibility(VISIBLE);
                    ic_cansel.setOnClickListener(v1 -> {
                        if (!parkingSpaceList.isEmpty()) {
                            adapter.updateData(parkingSpaceList);
                        }

                        ic_cansel.setVisibility(GONE);
                        ic_filter.setVisibility(VISIBLE);
                        Toast.makeText(context, "已取消筛选", Toast.LENGTH_SHORT).show();
                    });
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void filterData(int status, int type, Long parkLotId) {
        List<ParkingSpace> parkingSpaces = parkingSpaceService.filterData(status, type, parkLotId);
        if (parkingSpaces.isEmpty()) {
            Toast.makeText(context, "无符合条件的车位", Toast.LENGTH_SHORT).show();
        } else {
            adapter.updateData(parkingSpaces);
            Toast.makeText(context, "已筛选", Toast.LENGTH_SHORT).show();
        }

    }

}
